package saxion.speelveld.spaceengineer;

import saxion.speelveld.spaceengineer.database.AchievementDAO;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class StartScreen extends Activity
{
	Button NewGame;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_start_screen);
		NewGame = (Button) findViewById(R.id.btnNewGame);
		NewGame.setOnClickListener(new newGamebtnlistener());
		Button achievements = (Button) findViewById(R.id.btnAchievements);
		achievements.setOnClickListener(new achievementsButtonOnClickListener());
		Button loadGame = (Button) findViewById(R.id.btnLoadGame);
		loadGame.setOnClickListener(new loadButtonOnClickListener());

		putDummyAchievementIfNoneExist();
	}

	private void putDummyAchievementIfNoneExist()
	{
		AchievementDAO AchievementDatabase;
		AchievementDatabase = new AchievementDAO(this.getApplicationContext());
		AchievementDatabase.open();
		if (AchievementDatabase.hasAchievements() == false)
		{
			AchievementDatabase.putDummyAchievements();
		}
		AchievementDatabase.close();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_start_screen, menu);
		return true;
	}

	private class achievementsButtonOnClickListener implements OnClickListener
	{

		@Override
		public void onClick(View v)
		{
			Intent achievementListIntent = new Intent(v.getContext(), AchievementScreen.class);
			startActivity(achievementListIntent);

		}
	}

	private class loadButtonOnClickListener implements OnClickListener
	{

		@Override
		public void onClick(View arg0)
		{
			Intent buildingListIntent = new Intent(arg0.getContext(), LoadGamesScreen.class);
			startActivity(buildingListIntent);
		}

	}

	private class newGamebtnlistener implements OnClickListener
	{
		@Override
		public void onClick(View v)
		{
			LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
			View popup = inflater.inflate(R.layout.popup_layout, null);

			final Dialog dialog = new Dialog(v.getContext());
			dialog.setContentView(popup);
			dialog.setTitle("Select difficulty");
			dialog.show();

			// PopupWindow popupwindow = new PopupWindow(popup, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			// popupwindow.showAsDropDown(v);

			Button dif0 = (Button) popup.findViewById(R.id.dif0);// findViewById(R.id.dif0);
			Button dif1 = (Button) popup.findViewById(R.id.dif1);// findViewById(R.id.dif1);
			Button dif2 = (Button) popup.findViewById(R.id.dif2);// findViewById(R.id.dif2);
			Button dif3 = (Button) popup.findViewById(R.id.dif3);// findViewById(R.id.dif3);

			dif0.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					dialog.dismiss();
					launchGame(0);
				}
			});
			dif1.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					dialog.dismiss();
					launchGame(1);
				}
			});
			dif2.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					dialog.dismiss();
					launchGame(2);
				}
			});
			dif3.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					dialog.dismiss();
					launchGame(3);
				}
			});
		}

		private void launchGame(int dif)
		{
			Intent i = new Intent(getBaseContext(), GameScreen.class);
			i.putExtra("GameID", -1);
			i.putExtra("Difficultie", dif);
			startActivity(i);
		}
	}

}
